package cn.dataling.ai.component;

import cn.dataling.ai.util.LocalCache;
import cn.dataling.ai.util.RegexUtils;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

/**
 * @coder taoym
 * @datetime 2022/1/11 22:35
 * @desc
 */
@Configuration
@EnableScheduling
@EnableAsync
public class AccessTokenJob {

    @Value("${xcx.accessTokenUrl}")
    private String accessTokenUrl;
    private static final Logger LOGGER = LoggerFactory.getLogger(AccessTokenJob.class);
    @Resource
    private RestTemplate restTemplate;

    @Scheduled(fixedDelay = 7000 * 1000, initialDelay = 10)
    public void run() {
        String response = restTemplate.getForObject(accessTokenUrl, String.class);
        String accessToken = RegexUtils.matcher("\"access_token\":\"(.*?)\"", response, 1);
        LocalCache.set("REMOVE_WATER_MARK:XCX:ACCESS_TOKEN", accessToken);
        LOGGER.info("定时任务查询ACCESS_TOKEN结果为 {}", accessToken);
    }
}
